package com.olddriver.crm.util;

import com.olddriver.crm.domain.Menu;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;

import java.util.Iterator;
import java.util.List;

public abstract class MenuUtil {

    public static List<Menu> handleMenu(List<Menu> allMenus) {
        Iterator<Menu> iterator = allMenus.iterator();
        while (iterator.hasNext()) {
            Menu menu = iterator.next();
            Subject subject = SecurityUtils.getSubject();
            if (menu.getPermission() != null) {
                if (!subject.isPermitted(menu.getPermission().getResourse())) {
                    iterator.remove();
                    continue;
                }
            }
            if (menu.getChildren().size() > 0) {
                handleMenu(menu.getChildren());
            }
        }
        iterator = allMenus.iterator();
        while (iterator.hasNext()) {
            Menu menu = iterator.next();
            if (menu.getUrl() == null && menu.getChildren().size() == 0) {
                iterator.remove();
            }
        }
        return allMenus;
    }
}
